package com.yuju.controller.hotel;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.hotel.enums.BuildingReturnCodeEnum;
import com.yuju.hotel.model.BuildingModel;
import com.yuju.hotel.service.IBuildingService;
import com.yuju.util.Utils;
import com.yuju.vo.hotel.BuildingVO;
/**
 * 
 * 类名：BuildingController  <br />
 *
 * 功能：楼栋信息接口
 *
 * @author : zhousc <br />
 * @Date : 2017年11月27日 上午10:35:54  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("building")
public class BuildingController extends BaseController<BuildingVO, BuildingModel> {

	@Autowired
	private IBuildingService buildingService;
	
	/**
	 * 
	 * 功能：<br/>楼栋列表接口
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 上午10:36:47 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		BuildingVO buildingVO = obj.toJavaObject(BuildingVO.class);
		Result result = new Result();
		List<BuildingModel> list = buildingService.list(convert(buildingVO));
		if (Utils.isNotNull(list) && Utils.isNotNull(list)) {
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(BuildingReturnCodeEnum.BUILDING_QUERY_SUCCESS.getCode());
			result.setReturnMsg(BuildingReturnCodeEnum.BUILDING_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>新增楼栋
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 上午10:39:24 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		BuildingVO buildingVO = obj.toJavaObject(BuildingVO.class);
		Map<String, String> fail = FieldValidator.validateAll(buildingVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		Result result = new Result();
		int rows = buildingService.save(convert(buildingVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(BuildingReturnCodeEnum.BUILDING_SAVE_SUCCESS.getCode());
			result.setReturnMsg(BuildingReturnCodeEnum.BUILDING_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(BuildingReturnCodeEnum.BUILDING_SAVE_FAILURE.getCode());
			result.setReturnMsg(BuildingReturnCodeEnum.BUILDING_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>楼栋信息更新接口
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 上午10:39:41 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		BuildingVO buildingVO = obj.toJavaObject(BuildingVO.class);
		Map<String, String> fail = FieldValidator.validateAll(buildingVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		Result result = new Result();
		int rows = buildingService.updateSelective(convert(buildingVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(BuildingReturnCodeEnum.BUILDING_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(BuildingReturnCodeEnum.BUILDING_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(BuildingReturnCodeEnum.BUILDING_UPDATE_FAILURE.getCode());
			result.setReturnMsg(BuildingReturnCodeEnum.BUILDING_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>删除楼栋
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 上午10:40:08 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		BuildingVO buildingVO = obj.toJavaObject(BuildingVO.class);
		Map<String, String> fail = FieldValidator.validateAll(buildingVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		Result result = new Result();
		int rows = buildingService.del(buildingVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(BuildingReturnCodeEnum.BUILDING_DEL_SUCCESS.getCode());
			result.setReturnMsg(BuildingReturnCodeEnum.BUILDING_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(BuildingReturnCodeEnum.BUILDING_DEL_FAILURE.getCode());
			result.setReturnMsg(BuildingReturnCodeEnum.BUILDING_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>根据楼栋ID查询信息
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 上午10:40:21 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		BuildingVO buildingVO = obj.toJavaObject(BuildingVO.class);
		Map<String, String> fail = FieldValidator.validateAll(buildingVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		Result result = new Result();
		BuildingModel buildingModel = buildingService.findById(buildingVO.getId());
		if (Utils.isNotNull(buildingModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(buildingModel);
			result.setReturnCode(BuildingReturnCodeEnum.BUILDING_QUERY_SUCCESS.getCode());
			result.setReturnMsg(BuildingReturnCodeEnum.BUILDING_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：重名验证<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月14日 上午11:42:29 <br/>
	 */
	@RequestMapping("selectbyname")
	@ResponseBody
	public Result selectbyname(@RequestBody JSONObject obj) {
		BuildingVO buildingVO = obj.toJavaObject(BuildingVO.class);
		Map<String, String> fail = FieldValidator.validateAll(buildingVO, "selectbyname", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		};
		Result result = new Result();
		boolean selectByName = buildingService.selectByName(convert(buildingVO));
		if (selectByName) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
}
